#include "virtual_intc.h"

//虚拟中断控制器的中断域
static struct irq_domain *virtual_intc_domain;
//此中断控制器0号中断对应的上级中断控制器的中断编号
static uint32_t upper_hwirq_base;

static void virtual_intc_irq_ack(struct irq_data *data)
{
	printk("function:%s line:%d\n", __FUNCTION__, __LINE__);
	//调用上级中断控制器的irq_ack
	irq_chip_ack_parent(data);
}

static void virtual_intc_irq_mask(struct irq_data *data)
{
	printk("function:%s line:%d\n", __FUNCTION__, __LINE__);
	//调用上级中断控制器的irq_mask
	irq_chip_mask_parent(data);
}

static void virtual_intc_irq_mask_ack(struct irq_data *data)
{
	printk("function:%s line:%d\n", __FUNCTION__, __LINE__);
	//调用上级中断控制器的irq_mask_ack
	irq_chip_mask_ack_parent(data);
}

static void virtual_intc_irq_unmask(struct irq_data *data)
{
	printk("function:%s line:%d\n", __FUNCTION__, __LINE__);
	//调用上级中断控制器的irq_unmask
	irq_chip_unmask_parent(data);
}

static void virtual_intc_irq_eoi(struct irq_data *data)
{
	printk("function:%s line:%d\n", __FUNCTION__, __LINE__);
	//调用上级中断控制器的irq_eoi
	irq_chip_eoi_parent(data);
}

static struct irq_chip virtual_intc_irq_chip = {
	.name = "atk_virtual_intc",
	.irq_ack = virtual_intc_irq_ack,
	.irq_mask  = virtual_intc_irq_mask,
	.irq_mask_ack = virtual_intc_irq_mask_ack,
	.irq_unmask= virtual_intc_irq_unmask,
	.irq_eoi = virtual_intc_irq_eoi,
};

//解析设备树
static int virtual_intc_irq_translate(struct irq_domain *d, struct irq_fwspec *fwspec,
								unsigned long *out_hwirq, unsigned int *out_type)
{
	printk("function:%s line:%d\n", __FUNCTION__, __LINE__);

	//解析设备树
	if(fwspec->param_count != 2)
	{
		printk("The device tree format is incorrect\n");
		return -EINVAL;
	}
	*out_hwirq = fwspec->param[0];
	*out_type = fwspec->param[1] & IRQ_TYPE_SENSE_MASK;

	return 0;
}

//设置 irq_desc
static int virtual_intc_irq_alloc(struct irq_domain *d, unsigned int virq,
								unsigned int nr_irqs, void *arg)
{
	int i;
	unsigned long hwirq;
	unsigned int type;
	struct irq_fwspec *fwspec = arg;
	struct irq_fwspec parent_fwspec;

	printk("function:%s line:%d\n", __FUNCTION__, __LINE__);

	//解析设备树
	if(fwspec->param_count != 2)
	{
		printk("The device tree format is incorrect\n");
		return -EINVAL;
	}
	hwirq = fwspec->param[0];
	type = fwspec->param[1] & IRQ_TYPE_SENSE_MASK;

	//设置此级中断控制器域所对应的irq_chip
	for(i=0; i<nr_irqs; i++)
	{
		irq_domain_set_hwirq_and_chip(d, virq+i, hwirq+i, 
							&virtual_intc_irq_chip,
							NULL);
	}

	//组合上级中断控制器的IRQ说明符结构
	parent_fwspec.fwnode = d->parent->fwnode;
	parent_fwspec.param_count = 3;									//上级中断控制器需要3个参数
	parent_fwspec.param[0] = GIC_SPI;								//中断类型
	parent_fwspec.param[1] = fwspec->param[0] + upper_hwirq_base;	//物理中断号
	parent_fwspec.param[2] = fwspec->param[1];						//中断触发方式

	//调用上级中断控制器域的alloc函数，设置上级中断控制器域的irq_chip以及中断处理函数
	return irq_domain_alloc_irqs_parent(d, virq, 1, &parent_fwspec);
}

//执行与 alloc 相反的操作
static void virtual_intc_irq_free(struct irq_domain *d, unsigned int virq, unsigned int nr_irqs)
{
	printk("function:%s line:%d\n", __FUNCTION__, __LINE__);

	irq_domain_free_irqs_common(d, virq, nr_irqs);
}

static const struct irq_domain_ops virtual_intc_domain_ops = {
	.alloc = virtual_intc_irq_alloc,
	.free = virtual_intc_irq_free,
	.translate = virtual_intc_irq_translate,
};

//设备和驱动匹配成功执行
static int virtual_intc_probe(struct platform_device *pdev)
{
	int result;
	struct irq_domain *parent_domain;
	struct device_node *parent_irq_node;

	printk("function:%s line:%d\n", __FUNCTION__, __LINE__);

	//读取此中断控制器0号中断对应的上级中断控制器的中断编号
	result = of_property_read_u32(pdev->dev.of_node, "upper_hwirq_base", &upper_hwirq_base);
	if(result != 0)
	{
		printk("read upper_hwirq_base failed\r\n");
		return result;
	}

	//得到上级中断控制器的设备树节点
	parent_irq_node = of_irq_find_parent(pdev->dev.of_node);
	if(!parent_irq_node) 
	{
		printk("find parent irq node failed\r\n");
		return -ENXIO;
	}
	//得到上级中断控制器的域
	parent_domain = irq_find_host(parent_irq_node);
	of_node_put(parent_irq_node);
	if(!parent_domain)
	{
		printk("find parent irq host failed\r\n");
		return -EPROBE_DEFER;
	}

	//在上级中断控制器的域下面添加此中断控制器的域
	virtual_intc_domain = irq_domain_add_hierarchy(parent_domain, 0, 4,
													pdev->dev.of_node, &virtual_intc_domain_ops,
													NULL);
	if(!virtual_intc_domain) {
		pr_err("unable to add irq domain\n");
		return -ENODEV;
	}

	return 0;
}

//设备或驱动卸载时执行
static int virtual_intc_remove(struct platform_device *pdev)
{
	irq_domain_remove(virtual_intc_domain);
	return 0;
}

/* 匹配列表，用于设备树和平台驱动匹配 */
static const struct of_device_id virtual_intc_of_match[] = {
	{.compatible = "atk,virtual_intc"},
	{ /* Sentinel */ }
};
//平台驱动
static struct platform_driver virtual_intc_drv = {
	.driver = {
		.name = "virtual_intc",
		.owner = THIS_MODULE,
		.pm = NULL,
		.of_match_table = virtual_intc_of_match,
	},
	.probe = virtual_intc_probe,
	.remove = virtual_intc_remove,
};

static int __init virtual_intc_drv_init(void)
{
	int result;

	printk("%s\r\n", __FUNCTION__);

	//注册平台驱动
	result = platform_driver_register(&virtual_intc_drv);
	if(result < 0)
	{
		printk("add cdev faikey\r\n");
		return result;
	}

	return 0;
}

static void __exit virtual_intc_drv_exit(void)
{
	printk("%s\r\n", __FUNCTION__);

	//注销平台驱动
	platform_driver_unregister(&virtual_intc_drv);
}

module_init(virtual_intc_drv_init);
module_exit(virtual_intc_drv_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("LF");
MODULE_DESCRIPTION("virtual_intc_drivrer");
